// posts.js
var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');

Page({
  data: {
    title: '话题列表',
    postsList: [],
    hidden: false,
    page: 1,
    tab: 'all',
    loading: false
  },
  onLoad: function () {
    console.log('onLoad', new Date());
    this.fetchData();
  },
  onPullDownRefresh: function () {
    console.log('下拉刷新', new Date());
    this.data.page = 1;
    console.log(this.data.page);
    this.fetchData();
  },
  onReachBottom: function () {
    console.log('上拉加载更多', new Date());
    console.log(this.data.page);
    this.fetchData();
  },
  onTapTag: function (e) {
    var self = this;
    var tab = e.currentTarget.id;
    self.setData({
      tab: tab
    });
    if (tab !== 'all') {
      this.fetchData({ tab: tab });
    } else {
      this.fetchData();
    }
  },
  fetchData: function (data) {
    var self = this;
    if (self.data.loading) {
      return;
    }
    self.data.loading = true;
    if (!data) data = {};
    if (!data.page) data.page = 1;
    if (self.data.page === 1) {
      self.setData({
        postsList: []
      });
      self.setData({
        hidden: false
      });
    } else {
      wx.showNavigationBarLoading();
    }
    data.opt = 1;
    data.page = self.data.page;
    data.userId = util.getUserId();
    wx.request({
      url: Api.myTrial(data),
      header: Api.headers,
      success: function (res) {
        console.log("fetchData" + "-" + JSON.stringify(res));
        console.log("data" + !res.data.data);
        if (res.data.code == 0) {
          //   wx.showToast({
          //   title: res.data.msg,
          //   duration: 1500
          // })
        } else if (res.data.code != 1) {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              }
            }
          });
          return
        } else if (!res.data.data || res.data.data.length < 1) {
          wx.showToast({
            title: res.data.msg,
            duration: 1500
          })
          return;
        }
         console.log(" res.data.data " + res.data.data);
        if (self.data.postsList.length == 0 && (!(res.data.data&&(res.data.data instanceof Array)) || res.data.data.length < 1)) {
          wx.showModal({
            title: '提示',
            content: "您还没申请过试用,速去申请",
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.switchTab({
                  url: '/pages/trials/trials'
                })
              } else {
                console.log('用户点击取消')
                wx.navigateBack();
              }
            }
          });
          return;
        }
        console.log(self.data.postsList.length + " - " + res.data.data.length);
        self.setData({
          postsList: self.data.postsList.concat(res.data.data.map(function (item) {
            item.starttime = util.build_time(item.startTime);
            item.endtime = util.build_time(item.endTime);
            item.state = self.addTrailState(item);
            item.stateText = self.stateStrs[item.state][0];
            item.btnText = self.stateStrs[item.state][1];
            item.btnHidden = self.stateStrs[item.state][2];
            return item;
          }))
        });

        self.data.page++;
      },
      fail: function (res) {
        console.log('fail - ' + JSON.stringify(res));
      },
      complete: function (res) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh()
        self.data.loading = false;
        setTimeout(function () {
          self.setData({
            hidden: true
          });
        }, 300);
        console.log('complete - ' + JSON.stringify(res));
      },
    });
  },
  redictDetail: function (e) {
    var id = e.currentTarget.id,
      url = '../detail/detail?id=' + id;
    console.log('试用详情' + id);
    wx.navigateTo({
      url: url
    })
  },
  lower: function (e) {
    var self = this;
    self.setData({
      page: self.data.page + 1
    });
    if (self.data.tab !== 'all') {
      this.fetchData({ tab: self.data.tab, page: self.data.page });
    } else {
      this.fetchData({ page: self.data.page });
    }
  },
  //试用结果
  stateStrs: [
    ["已获奖", "查看报告", true, "移驾瑞丽美妆app上查看报告"],
    ["已获奖", "提交报告", true, "移驾瑞丽美妆app上提交试用报告"],
    ["已提交申请", "", false],
    ["已结束", "", false]
  ],
  //添加试用申请进度状态
  addTrailState: function (value) {
    var awards = value.awards;
    var isreport = value.isreport;
    var is_end = value.is_end;
    var state = 1;
    //
    /*
     /var stateStrs = [
     ["已获奖","查看报告"],
     ["已获奖","提交报告"],
     ["已提交申请",""],
     ["已结束",""]
     ]
     */
    // var stateStrs = [];
    if (awards == 1) {
      if (isreport == 1) {
        state = 0;
      } else {
        state = 1;
      }
    } else {
      if (is_end == 1) {
        state = 3;
      } else {
        state = 2;
      }
    }
    // value.state = state;
    return state;
  }
})
